package com.zlsy.es.dianping.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlsy.es.dianping.annotation.AdminPermission;
import com.zlsy.es.dianping.base.R;
import com.zlsy.es.dianping.pojo.model.Seller;
import com.zlsy.es.dianping.service.SellerService;
import com.zlsy.es.dianping.pojo.vo.request.PageQueryRequest;
import com.zlsy.es.dianping.pojo.vo.request.SellerCreateRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhouliang
 */
@Controller
@RequestMapping("/admin/seller")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SellerController {

    private final SellerService sellerService;

    /**
     * 商户列表
     *
     * @param pageQuery
     * @return
     */
    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(PageQueryRequest pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<Seller> sellerModelList = sellerService.selectAll();
        PageInfo<Seller> sellerModelPageInfo = new PageInfo<>(sellerModelList);
        ModelAndView modelAndView = new ModelAndView("/admin/seller/index.html");
        modelAndView.addObject("data", sellerModelPageInfo);
        modelAndView.addObject("CONTROLLER_NAME", "seller");
        modelAndView.addObject("ACTION_NAME", "index");
        return modelAndView;
    }

    @RequestMapping("/createpage")
    @AdminPermission
    public ModelAndView createPage() {
        ModelAndView modelAndView = new ModelAndView("/admin/seller/create.html");
        modelAndView.addObject("CONTROLLER_NAME", "seller");
        modelAndView.addObject("ACTION_NAME", "create");
        return modelAndView;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @AdminPermission
    public String create(@Valid SellerCreateRequest sellerCreateReq) {
        Seller sellerModel = new Seller();
        sellerModel.setName(sellerCreateReq.getName());
        sellerService.create(sellerModel);
        return "redirect:/admin/seller/index";


    }

    @RequestMapping(value = "down", method = RequestMethod.POST)
    @AdminPermission
    @ResponseBody
    public R<Seller> down(@RequestParam(value = "id") Integer id) {
        Seller sellerModel = sellerService.changeStatus(id, 1);
        return R.success(sellerModel);
    }

    @RequestMapping(value = "up", method = RequestMethod.POST)
    @AdminPermission
    @ResponseBody
    public R<Seller> up(@RequestParam(value = "id") Integer id) {
        Seller sellerModel = sellerService.changeStatus(id, 0);
        return R.success(sellerModel);
    }
}
